const pieces = document.querySelectorAll('.piece');
const squares = document.querySelectorAll('.square');
// console.log(pieces, '///');
// console.log(squares, '...');
let draggedPiece = null;

// 遍历监听每个被拖拽的棋子
for (let i = 0; i < pieces.length; i++) {
	pieces[i].addEventListener('dragstart', function(event) {
		// console.log('id=', event.target.id);
		draggedPiece = event.target.id; //标签的ID
		// draggedPiece = event.target.id;
	});
}

// 遍历监听每个空的棋盘格子
for (let i = 0; i < squares.length; i++) {
	squares[i].addEventListener('drop', function(event) {
		event.preventDefault(); //阻止默认事件
		// console.log('draggedPiece=', draggedPiece);
		const piece = document.getElementById(draggedPiece);
		// console.log('piece=', piece);
		if (event.target.children.length > 0) {
			return;
		}
		const squareIndex = Array.from(squares).indexOf(event.target);
		const pieceIndex = Array.from(pieces).indexOf(piece);
		if (isValidMove(squareIndex, pieceIndex)) {
			event.target.appendChild(piece);
		}
	});
	squares[i].addEventListener('dragover', function(event) {
		event.preventDefault();
	});
}

function isValidMove(squareIndex, pieceIndex) { //判断空位置
	// 实现游戏规则
	return true;
}